home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / sossnt.zip / SOSSNT / SRC / PMAP.C < prev    next >
C/C++ Source or Header  |  1993-03-03  |  4KB  |  153 lines

  1. /*
  2.  *  pmap.c --
  3.  *      Portmapper program for PC NFS file server.
  4.  *      Service runs on top of the net daemon (netd.c).
  5.  *      This is a partial implementation of the UNIX port mapper
  6.  *      portmap(8c).
  7.  *
  8.  *  Author:
  9.  *      See-Mong Tan, 6/12/88
  10.  *      portmapper bug fixed 3/23/89
  11.  */
  12.  
  13. #include "common.h"
  14.  
  15. /*
  16.  *  bool_t pmap_init() --
  17.  *      Portmapper initializer.  Called by the net daemon.  Creates
  18.  *      and registers it's own transport handler and dispatch routine.
  19.  *      Returns TRUE if successful, or FALSE if an error occurred.
  20.  */
  21. bool_t pmap_init()
  22. {
  23.     SVCXPRT *transp;
  24.     /* struct sockaddr_in addr; */
  25.     SOCKET sock;
  26.     SOCKADDR_IN local_sin;
  27.  
  28.     sock = socket(PF_INET, SOCK_DGRAM, 0);
  29.  
  30.     if (sock == INVALID_SOCKET) {
  31.         (void) fprintf(stderr, "cannot create pmap socket\n");
  32.         return FALSE;
  33.     }
  34.  
  35.     local_sin.sin_family = AF_INET;
  36.     local_sin.sin_port = htons(PMAPPORT);
  37.     local_sin.sin_addr.s_addr = INADDR_ANY;
  38.  
  39.       if (bind( sock, (struct sockaddr FAR *) &local_sin, sizeof(local_sin)) == SOCKET_ERROR) {
  40.         fprintf(stderr,"bind() failed");
  41.         return FALSE;
  42.     }
  43.  
  44.     if ((transp = svcudp_create(sock, 0,local_sin.sin_port)) == (SVCXPRT *) NULL)
  45.         return FALSE;
  46.  
  47.     if (! svc_register(transp, PMAPPROG, PMAPVERS, 
  48.                 pmap_dispatch, IPPROTO_UDP)) {
  49.         (void) fprintf(stderr, "pmap_init: cannot register handle\n");
  50.         closesocket(sock);
  51.         return FALSE;
  52.     }
  53.     return TRUE;
  54. }
  55.         
  56. /*
  57.  *  void pmap_dispatch(struct svc_req *rqstp, SVCXPRT *transp) --
  58.  *      Dispatch routine for portmapper.  This is only a partial
  59.  *      implementation, in particular, PMAPPROC_SET and PMAPPROC_UNSET
  60.  *      are not supported.
  61.  */
  62. void pmap_dispatch(rqstp, transp)
  63.     struct svc_req *rqstp;
  64.     SVCXPRT *transp;
  65. {
  66.     int NOTSUPP = 0;     /* error not supported */
  67.     struct pmap reg;
  68.     int port;
  69.  
  70.     switch((int) rqstp->rq_proc) {
  71.  
  72.     case PMAPPROC_NULL:   /* NULL procedure call */
  73.         if (NFS_VERBOSE)
  74.             (void) printf(">>> PMMAPPROC_NULL\n");
  75.         if (! svc_sendreply(transp, xdr_void, NULL))
  76.             (void) fprintf(stderr, "pmap: cannot send reply\n");
  77.         break;
  78.  
  79.     case PMAPPROC_SET:   /* these reqeusts are not supported */
  80.     case PMAPPROC_UNSET:
  81.         if (NFS_VERBOSE)
  82.             (void) printf("pmap: PMAPPROC_SET/UNSET\n");
  83.         if (! svc_sendreply(transp, xdr_int, &NOTSUPP))
  84.             (void) fprintf(stderr, "pmap: cannot send reply\n");
  85.         break;
  86.  
  87.     case PMAPPROC_GETPORT:  /* get a port */
  88.         if (NFS_VERBOSE)
  89.             (void) printf("\n>>> PMAPPROC_GETPORT\t"); 
  90.         if (! svc_getargs(transp, xdr_pmap, ®)) {
  91.             svcerr_decode(transp);
  92.             break;
  93.         }
  94.         if (NFS_VERBOSE)
  95.             (void) printf("--> prog = %ld, vers = %ld:\t", 
  96.                       reg.pm_prog, reg.pm_vers);
  97.         port = 0;
  98.         if (reg.pm_prog == NFS_PROGRAM && reg.pm_vers == NFS_VERSION) {
  99.             port = NFS_PORT;
  100.             if (NFS_VERBOSE)
  101.                 (void) printf(" --> NFS_PORT\n");
  102.         }
  103.         if (reg.pm_prog== MOUNTPROG ) {
  104.             if ( reg.pm_vers== MOUNTVERS || 
  105.                  reg.pm_vers == MOUNTVERS_OLD ) {
  106.                 port = MOUNTPORT;
  107.                 if (NFS_VERBOSE)
  108.                     (void) printf("--> MOUNTPORT\n");
  109.             }
  110.             else {
  111.                 (void) printf("--> Client mount versio should be %d\n",
  112.                     MOUNTVERS);
  113.             }
  114.         }
  115.         else {    /* everything else is not supported */
  116.             if(NFS_VERBOSE)
  117.                 (void) printf("--> NO PORT REGISTERED\n");
  118.         }
  119.         if (! svc_sendreply(transp, xdr_int, (caddr_t)&port))
  120.             (void) fprintf(stderr, "cannot reply\n");
  121.         break;
  122.  
  123.         /* The fix  for the portmapper bug discovered by
  124.          * Stephen Nahm follows
  125.          */
  126.     case PMAPPROC_CALLIT:
  127.         if (NFS_VERBOSE)
  128.             (void) printf(">>> PMAPPROC_DUMP/PMAPPROC_CALLIT\n");
  129.         break;    /* should break out and not reply */
  130.         
  131.     case PMAPPROC_DUMP:
  132.         /* FALLTHROUGH */
  133.     default:
  134.         svcerr_noproc(transp);
  135.     }
  136. }
  137.  
  138. /*
  139.  *  bool_t xdr_pmap(XDR *xdrs, struct pmap *regs) --
  140.  *      XDR a pmap request.
  141.  */
  142. bool_t xdr_pmap(xdrs, regs)
  143.     XDR *xdrs;
  144.     struct pmap *regs;
  145. {
  146.  
  147.     if (xdr_u_long(xdrs, ®s->pm_prog) && 
  148.         xdr_u_long(xdrs, ®s->pm_vers) && 
  149.         xdr_u_long(xdrs, ®s->pm_prot))
  150.         return (xdr_u_long(xdrs, ®s->pm_port));
  151.     return (FALSE);
  152. }
  153.